package y23.Nov;

import java.io.File;
import java.util.Scanner;

public class Test4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的路径: ");
        String path = scanner.next();
        File rootPath = new File(path);
        if (!rootPath.isDirectory()) {
            System.out.println("没有该路径");
            return;
        }
        System.out.println("请输入要删除文件的关键词: ");
        String word = scanner.next();
        scanDir(rootPath, word);

    }

    private static void scanDir(File rootPath, String word) {
        File[] files = rootPath.listFiles();
        if (files == null) {
            return;
        }
        for (File f : files) {
            if (f.isFile()) {
                checkDelete(f, word);
            } else {
                scanDir(f, word);
            }
        }
    }

    private static void checkDelete(File f, String word) {
        if (!f.getName().contains(word)) {
            return;
        }
        System.out.println(f.getAbsolutePath() +  "    是否删除该文件(Y/N): ");
        Scanner scanner = new Scanner(System.in);
        String choice = scanner.next();
        if (choice.equals("Y") || choice.equals("y")) {
            f.delete();
            System.out.println("文件已删除");
        } else {
            System.out.println("取消删除!");
        }
    }
}
